package zhieasy.cn.service.found.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhieasy.cn.model.found.FoundCommentRely;
import zhieasy.cn.model.found.FoundCommentRoot;
import zhieasy.cn.persistence.mapper.found.FoundCommentRelyMapper;
import zhieasy.cn.persistence.mapper.found.FoundCommentRootMapper;
import zhieasy.cn.service.found.FoundCommentRootService;

import java.util.HashMap;
import java.util.List;

/**
 * @author wang zeya
 * @date 2020/8/29 16:38
 */
@Service
public class FoundCommentRootServiceImpl extends ServiceImpl<FoundCommentRootMapper, FoundCommentRoot> implements FoundCommentRootService {

    @Autowired
    private FoundCommentRootMapper foundCommentRootMapper;
    @Autowired
    private FoundCommentRelyMapper foundCommentRelyMapper;

    public List<FoundCommentRoot> getCommonRootByFoundId(String lafId, int pageIndex, int pageSize) {


        QueryWrapper<FoundCommentRoot> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("owner_id", lafId);
        queryWrapper.orderByDesc("create_time");
        Page<FoundCommentRoot> myPage = new Page<>(pageIndex, pageSize);
        IPage<FoundCommentRoot> page = foundCommentRootMapper.selectPage(myPage, queryWrapper);
        List<FoundCommentRoot> rootCommons = page.getRecords();

        //拿到数据了，那就要把这些数据里面的所有子评论拿出来
        rootCommons.forEach(common -> {
            //获取一下id
            int id = common.getId();
            //然后通过这个id把关于他的评论都找出来
            List<FoundCommentRely> foundCommentRelyList = foundCommentRelyMapper.selectByMap(new HashMap<String, Object>() {{
                put("comment_id", id);
            }});
            foundCommentRelyList.stream().forEach(foundCommonRely -> {
                if (foundCommonRely.getValid() == 0) {
                    foundCommonRely.setContent("该内容已被折叠~");
                }
            });
            common.setFoundCommentRelies(foundCommentRelyList);
        });
        return rootCommons;
    }


}
